<?php
/* SVN FILE: $Id: statistics_players_controller.php 325 2010-07-19 18:37:40Z gregory.rome $ */
/**
 * StatisticsPlayers Controller.
 *
 * cBBweb : Digital Blood Bowl league interface (http://code.google.com/p/cbbweb/)
 * Copyright 2009-2010, Grégory Romé.
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @filesource
 * @copyright     Copyright 2009-2010, Grégory Romé.
 * @link          http://code.google.com/p/cbbweb/ cBBweb Project
 * @package       cbbweb
 * @subpackage    cbbweb.controllers
 * @version       $Revision: 325 $
 * @modifiedby    $LastChangedBy: gregory.rome $
 * @lastmodified  $Date: 2010-07-19 18:37:40 +0000 (Mon, 19 Jul 2010) $
 * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
 */

/**
 * StatisticsPlayers Controller.
 *
 * @package       cbbweb
 * @subpackage    cbbweb.controllers
 */
class StatisticsPlayersController extends AppController {

	var $name = 'StatisticsPlayers';
	var $paginate = array(
  	'StatisticsPlayer' => array(
  		'order' => array(
				'StatisticsPlayer.Inflicted_iTouchdowns' => 'desc',
				'Player.iExperience' => 'desc',
				'Player.iValue' => 'desc'),
			'limit' => 10,
  		'contain' => array(
				'Player' => array(
					'fields' => array(
						'Player.ID',
						'Player.strName',
						'Player.idTeam_Listing',
						'Player.iValue',
						'Player.iExperience'),
					'Team' => array(
						'fields' => array('Team.ID, Team.strName'),
						'Race' => array(
							'LocalizedString'),
						'Coach'),
					'PlayerType' => array(
						'fields' => array('PlayerType.ID'),
						'LocalizedString')))));

	/**
	 * @todo Description
	 */
	function index()
	{
		$options = false;
		if ($this->championship)
		{
			$result = $this->StatisticsPlayer->Player->find('list', array(
				'fields' => array('Player.ID'),
				'conditions' => array(
					'Player.idTeam_Listing' => $this->current['Teams'])));

			$options['StatisticsPlayer.idPlayer_Listing'] = $result;
		}
		$statisticsPlayers = $this->paginate('StatisticsPlayer', $options);

		if (isset ($this->params['requested'])) {
			return $statisticsPlayers;
		} else {
			$this->set('statisticsPlayers', $statisticsPlayers);
		}

	}

	/** Get Best Players.
	 * @param field
	 * @param limit
	 */
	function get_bestplayers($field='td', $limit=5)
	{
		if(isset($this->passedArgs['limit'])) {
			$limit = $this->passedArgs['limit'];
		}
		if(isset($this->passedArgs['field'])) {
			$field = $this->passedArgs['field'];
		}

		switch ($field) {
			case 'td':
				$field = 'Inflicted_iTouchdowns';
				$fieldName = __('TD', true);
				$fieldTitle = __('Touchdowns', true);
				break;
			case 'pa':
				$field = 'Inflicted_iPasses';
				$fieldName = __('Pa', true);
				$fieldTitle = __('Passes', true);
				break;
			case 'inj':
				$field = 'Inflicted_iInjuries';
				$fieldName = __('Inj', true);
				$fieldTitle = __('Injuries', true);
				break;
			case 'mr':
				$field = 'Inflicted_iMetersRunning';
				$fieldName = __('MR', true);
				$fieldTitle = __('Meters Running', true);
				break;
			case 'cas':
				$field = 'Inflicted_iCasualties';
				$fieldName = __('Cas', true);
				$fieldTitle = __('Casualties', true);
				break;
			case 'dd':
				$field = 'Inflicted_iDead';
				$fieldName = __('Dd', true);
				$fieldTitle = __('Deads', true);
				break;
			case 'mp':
				$field = 'Inflicted_iMetersPassing';
				$fieldName = __('MP', true);
				$fieldTitle = __('Meters Passing', true);
				break;
			case 'ca':
				$field = 'Inflicted_iCatches';
				$fieldName = __('Ca', true);
				$fieldTitle = __('Catches', true);
				break;
			case 'int':
				$field = 'Inflicted_iInterceptions';
				$fieldName = __('Int', true);
				$fieldTitle = __('Interceptions', true);
				break;
			case 'sinj':
				$field = 'Sustained_iInjuries';
				$fieldName = __('sInj', true);
				$fieldTitle = __('Sustained Injuries', true);
				$fieldOrder = 'asc';
				break;
			case 'scas':
				$field = 'Sustained_iCasualties';
				$fieldName = __('sCas', true);
				$fieldTitle = __('Sustained Casualties', true);
				$fieldOrder = 'asc';
				break;
			default:
				$this->Session->setFlash(__('Wrong field', true));
				return;
		}

		if( !isset($fieldOrder)) {
			$fieldOrder = 'desc';
			$options['StatisticsPlayer.'.$field.' <>'] = 0;
		}

		if ($this->championship)
		{
			$result = $this->StatisticsPlayer->Player->find('list', array(
				'fields' => array('Player.ID'),
				'conditions' => array(
					'Player.idTeam_Listing' => $this->current['Teams'])));

			$options['StatisticsPlayer.idPlayer_Listing'] = $result;
		}

		$statisticsPlayers = $this->StatisticsPlayer->find('all', array(
			'fields' => array('StatisticsPlayer.'.$field, 'StatisticsPlayer.idPlayer_Listing'),
			'order' => array(
				'StatisticsPlayer.'.$field => $fieldOrder,
				'Player.iValue' => 'desc',
				'Player.iExperience' => 'desc',
			),
			'limit' => $limit,
			'contain' => array(
				'Player' => array(
					'fields' => array(
						'Player.ID',
						'Player.strName',
						'Player.idTeam_Listing',
						'Player.iValue',
						'Player.iExperience'),
					'Team' => array(
						'fields' => array('Team.ID', 'Team.strName', 'Team.idCoach'),
						'Coach',
						'Race' => array(
							'LocalizedString')),
					'PlayerType' => array(
						'fields' => array('PlayerType.ID'),
						'LocalizedString'))),
			'conditions' => $options,
		));

		if($this->coach) {
			$coachHasPlayer = false;
			foreach($statisticsPlayers as $statisticsPlayer) {
				if(!empty($statisticsPlayer['Player']['Team']['idCoach']) &&
					$statisticsPlayer['Player']['Team']['idCoach'] == $this->coach) {
						$coachHasPlayer = true;
				}
			}

			if(!$coachHasPlayer) {

				$options['Player.ID'] = $this->current['Coach']['Players'];
				$coachStatisticsPlayer = $this->StatisticsPlayer->find('first', array(
					'fields' => array('StatisticsPlayer.'.$field, 'StatisticsPlayer.idPlayer_Listing'),
					'order' => array(
						'StatisticsPlayer.'.$field => $fieldOrder,
						'Player.iValue' => 'desc',
						'Player.iExperience' => 'desc',
					),
					'contain' => array(
						'Player' => array(
							'fields' => array(
								'Player.ID',
								'Player.strName',
								'Player.idTeam_Listing',
								'Player.iValue',
								'Player.iExperience'),
							'Team' => array(
								'fields' => array('Team.ID', 'Team.strName', 'Team.idCoach'),
								'Coach',
								'Race' => array(
									'LocalizedString')),
							'PlayerType' => array(
								'fields' => array('PlayerType.ID'),
								'LocalizedString'))),
					'conditions' => $options,
				));

				unset($options['Player.ID']);
				$results = $this->StatisticsPlayer->find('all', array(
					'fields' => array('StatisticsPlayer.idPlayer_Listing'),
					'order' => array(
						'StatisticsPlayer.'.$field => $fieldOrder,
						'Player.iValue' => 'desc',
						'Player.iExperience' => 'desc',
					),
					'conditions' => $options,
				));

				$rank = 0;
				foreach($results as $player) {
					$rank++;
					if($player['StatisticsPlayer']['idPlayer_Listing'] ==
							$coachStatisticsPlayer['StatisticsPlayer']['idPlayer_Listing']) {
						$coachStatisticsPlayer['Rank'] = $rank;
					}
				}
				$this->set('coachStatisticsPlayer', $coachStatisticsPlayer);
			}
		}

		if (isset ($this->params['requested'])) {
			return $statisticsPlayers;
		} else {

			$this->set('statisticsPlayers', $statisticsPlayers);
			$this->set('field', $field);
			$this->set('fieldName', $fieldName);
			$this->set('fieldTitle', $fieldTitle);
		}
	}

}
?>